#/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License, version 3.
# See the file http://www.gnu.org/licenses/gpl.txt

from inary.actionsapi import get
from inary.actionsapi import autotools
from inary.actionsapi import inarytools
from inary.actionsapi import shelltools
import os


def setup():
    CONFIGPARAMS = "--prefix=/usr \
            --with-shared   \
            --with-debug         \
            --with-normal        \
            --enable-widec          \
            --with-cxx-binding      \
            --with-cxx-shared       \
            --with-shared           \
            --enable-pc-files       "
    if get.buildTYPE() == "emul32":
        shelltools.export("CC","{} -m32".format(get.CC()))
        shelltools.export("CXX","{} -m32".format(get.CXX()))
        CONFIGPARAMS += "--with-pkg-config-libdir=/lib32/pkgconfig \
                         --libdir=/lib32 --bindir=/emul32"
    else:
        CONFIGPARAMS += "--with-pkg-config-libdir=/lib/pkgconfig --libdir=/lib --bindir=/bin"
    autotools.rawConfigure("--enable-widec --enable-pc-files {}".format(CONFIGPARAMS))


def build():
    autotools.make()

def install():
    autotools.rawInstall("DESTDIR=%s" % get.installDIR())
    LIB = "/lib32" if get.buildTYPE() == "emul32" else "/lib"

    for lib in ["ncurses", "form", "panel", "menu"]:
        shelltools.echo("lib%s.so" % lib, "INPUT(-l%sw)" % lib)
        inarytools.dolib("lib%s.so" % lib, destinationDirectory = LIB)
        #inarytools.dosym("lib%sw.a" % lib, "%s/lib%s.a" % (LIB, lib))
    inarytools.dosym("libncurses++w.a", "%s/libncurses++.a" % LIB)

    for lib in ["ncurses", "ncurses++", "form", "panel", "menu"]:
        inarytools.dosym("%sw.pc" % lib, "%s/pkgconfig/%s.pc" % (LIB, lib))

    shelltools.echo("libcursesw.so", "INPUT(-lncursesw)")
    #inarytools.dolib("libcursesw.so", destinationDirectory = LIB)
    #inarytools.dosym("libncurses.so", "%s/libcurses.so" % LIB)
    #inarytools.dosym("libncursesw.a", "%s/libcursesw.a" % LIB)
    #inarytools.dosym("libncurses.a", "%s/libcurses.a" % LIB)

    #for fix
    inarytools.dosym("libncursesw.so.6", "%s/libncursesw.so.5" % LIB)
    inarytools.dosym("libncurses.so.6", "%s/libncurses.so.5" % LIB)
    inarytools.dosym("libncurses.so.6", "%s/libtinfo.so.5" % LIB)
    inarytools.dosym("libpanelw.so.6", "%s/libpanelw.so.5" % LIB)
    inarytools.dosym("libformw.so.6", "%s/libformw.so.5" % LIB)
    inarytools.dosym("libmenuw.so.6", "%s/libmenuw.so.5" % LIB)
    inarytools.dosym("libncurses.so", "%s/libtinfo.so" % LIB)

    for lib in ["libtinfo", "libncurses"]:
        inarytools.dosym("{}.so".format(lib), "{}/{}.so.6".format(LIB,lib))
        inarytools.dosym("{}.so".format(lib), "{}/{}.so.6.2".format(LIB,lib))

    if get.buildTYPE() == "emul32":
        if os.path.exists("{}/emul32".format(get.installDIR())):
            inarytools.removeDir("/emul32")
        return

    
    shelltools.system("grep -B 100 '$Id' README > license.txt")
    inarytools.dodoc("ANNOUNCE", "NEWS", "README*", "TO-DO", "license.txt")
